#include <sxOpenGL_DefaultCuboidRenderer.h>
#include <sxOpenGL.h>
#include <gl/glut.h>

Sx::OpenGL::Graphics::Objects::Rendering::OpenGL_DefaultCuboidRenderer::OpenGL_DefaultCuboidRenderer(const std::shared_ptr<OpenGL_Context>& graphicsContext) : GraphicsObjectRenderer(graphicsContext) {

}

Sx::OpenGL::Graphics::Objects::Rendering::OpenGL_DefaultCuboidRenderer::~OpenGL_DefaultCuboidRenderer() {}

bool Sx::OpenGL::Graphics::Objects::Rendering::OpenGL_DefaultCuboidRenderer::construct(OpenGL_Context* const graphicsContext, Cuboid* const cuboid, StaticRepresentation* const physicalRepresentation) {
	return true;
}

void Sx::OpenGL::Graphics::Objects::Rendering::OpenGL_DefaultCuboidRenderer::update(OpenGL_Context* const graphicsContext, Cuboid* const cuboid, float dt) {
	return;
}

void Sx::OpenGL::Graphics::Objects::Rendering::OpenGL_DefaultCuboidRenderer::render(OpenGL_Context* const graphicsContext, Cuboid* const cuboid, StaticRepresentation* const physicalRepresentation) {
	
	
	glPushMatrix();
	glMultMatrixf(cuboid->getTransformation().toMatrix4f().data());

	glDisable(GL_LIGHTING);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);

	if ( cuboid->isSelected() ) glColor4f(0.6f, 0.7f, 0.9f, 0.8f);
	else glColor4f(0.4f, 0.5f, 0.7f, 0.8f);
	glBegin(GL_TRIANGLES);
	//s1
		glVertex3fv(cuboid->getCorner(true, true, false).data());
		glVertex3fv(cuboid->getCorner(true, false, false).data());
		glVertex3fv(cuboid->getCorner(true, false, true).data());

		glVertex3fv(cuboid->getCorner(true, false, true).data());
		glVertex3fv(cuboid->getCorner(true, true, false).data());
		glVertex3fv(cuboid->getCorner(true, true, true).data());

	//s2
		glVertex3fv(cuboid->getCorner(false, true, false).data());
		glVertex3fv(cuboid->getCorner(false, false, false).data());
		glVertex3fv(cuboid->getCorner(false, false, true).data());

		glVertex3fv(cuboid->getCorner(false, false, true).data());
		glVertex3fv(cuboid->getCorner(false, true, false).data());
		glVertex3fv(cuboid->getCorner(false, true, true).data());

	//s3
		glVertex3fv(cuboid->getCorner(false, true, false).data());
		glVertex3fv(cuboid->getCorner(true, true, false).data());
		glVertex3fv(cuboid->getCorner(true, true, true).data());

		glVertex3fv(cuboid->getCorner(false, true, false).data());
		glVertex3fv(cuboid->getCorner(true, true, true).data());
		glVertex3fv(cuboid->getCorner(false, true, true).data());

	//s4
		glVertex3fv(cuboid->getCorner(false, false, false).data());
		glVertex3fv(cuboid->getCorner(true, false, false).data());
		glVertex3fv(cuboid->getCorner(true, false, true).data());

		glVertex3fv(cuboid->getCorner(false, false, false).data());
		glVertex3fv(cuboid->getCorner(true, false, true).data());
		glVertex3fv(cuboid->getCorner(false, false, true).data());

	//s5
		glVertex3fv(cuboid->getCorner(true, true, true).data());
		glVertex3fv(cuboid->getCorner(false, true, true).data());
		glVertex3fv(cuboid->getCorner(false, false, true).data());

		glVertex3fv(cuboid->getCorner(false, false, true).data());
		glVertex3fv(cuboid->getCorner(true, false, true).data());
		glVertex3fv(cuboid->getCorner(true, true, true).data());

	//s6
		glVertex3fv(cuboid->getCorner(true, true, false).data());
		glVertex3fv(cuboid->getCorner(false, true, false).data());
		glVertex3fv(cuboid->getCorner(false, false, false).data());

		glVertex3fv(cuboid->getCorner(false, false, false).data());
		glVertex3fv(cuboid->getCorner(true, false, false).data());
		glVertex3fv(cuboid->getCorner(true, true, false).data());

	glEnd();

	/*
	if ( cuboid->isSelected() ) glColor4f(0.7f, 0.8f, 1.0f, 1.0f);
	else glColor4f(0.5f, 0.6f, 0.8f, 1.0f);
	glPointSize(5.0f);
	glBegin(GL_POINTS);
		glVertex3fv(cuboid->getCorner(false, false, false).data());
		glVertex3fv(cuboid->getCorner(false, false, true).data());
		glVertex3fv(cuboid->getCorner(false, true, false).data());
		glVertex3fv(cuboid->getCorner(false, true, true).data());
		glVertex3fv(cuboid->getCorner(true, false, false).data());
		glVertex3fv(cuboid->getCorner(true, false, true).data());
		glVertex3fv(cuboid->getCorner(true, true, false).data());
		glVertex3fv(cuboid->getCorner(true, true, true).data());
	glEnd();
	*/

	glDisable(GL_DEPTH_TEST);
	if ( cuboid->isSelected() ) glColor4f(0.3f, 0.4f, 1.0f, 0.8f);
	else glColor4f(0.1f, 0.2f, 0.8f, 0.8f);
	glBegin(GL_LINES);
		glVertex3fv(cuboid->getCorner(false, false, false).data());
		glVertex3fv(cuboid->getCorner(false, false, true).data());
		glVertex3fv(cuboid->getCorner(false, true, false).data());
		glVertex3fv(cuboid->getCorner(false, true, true).data());
		glVertex3fv(cuboid->getCorner(false, false, true).data());
		glVertex3fv(cuboid->getCorner(false, true, true).data());
		glVertex3fv(cuboid->getCorner(false, false, false).data());
		glVertex3fv(cuboid->getCorner(false, true, false).data());

		glVertex3fv(cuboid->getCorner(false, false, false).data());
		glVertex3fv(cuboid->getCorner(true, false, false).data());
		glVertex3fv(cuboid->getCorner(false, true, false).data());
		glVertex3fv(cuboid->getCorner(true, true, false).data());
		glVertex3fv(cuboid->getCorner(false, true, true).data());
		glVertex3fv(cuboid->getCorner(true, true, true).data());
		glVertex3fv(cuboid->getCorner(false, false, true).data());
		glVertex3fv(cuboid->getCorner(true, false, true).data());

		glVertex3fv(cuboid->getCorner(true, false, false).data());
		glVertex3fv(cuboid->getCorner(true, false, true).data());
		glVertex3fv(cuboid->getCorner(true, true, false).data());
		glVertex3fv(cuboid->getCorner(true, true, true).data());
		glVertex3fv(cuboid->getCorner(true, false, true).data());
		glVertex3fv(cuboid->getCorner(true, true, true).data());
		glVertex3fv(cuboid->getCorner(true, false, false).data());
		glVertex3fv(cuboid->getCorner(true, true, false).data());
	glEnd();
	glPopMatrix();

	glEnable(GL_LIGHTING);
	glEnable(GL_DEPTH_TEST);
	glDisable(GL_BLEND);
}
